/**
 * 
 */
package cn.antia.cwin.grid;

import java.util.*;

import org.dom4j.*;
import org.dom4j.io.SAXReader;

import cn.antia.cep.common.*;
import cn.antia.validation.Validation;
import cn.antia.cwin.window.WindowConfiger;
import cn.antia.cwin.*;

/**
 * @author sunkeei
 * @date Nov 19, 2007
 * @time 10:47:11 PM
 * @version 0.1
 */
public class GridConfiger {
	private static Document doc;
	public static int PAGE_SIZE;
	
	static{
		reset();
	}
	
	/**
	 * 重置
	 */
	public static void reset(){
		SAXReader reader = new SAXReader();
		try{
			doc = reader.read(SysConf.SYSTEM_GRID_CONFIG);
			Element element = (Element)doc.selectSingleNode("/all-grid/config");
			String sv = element.attributeValue("pageSize");
			if(sv != null){
				PAGE_SIZE = Integer.parseInt(sv);
			}
		}catch(DocumentException de){
			AntiaLog.fatal("DBConfiger init error.");
			de.printStackTrace();
		}
	}
	
	/**
	 * 返回表名
	 * @param gridId
	 * @return
	 */
	public static String getSingleTable(String gridId){
		StringBuffer sb = new StringBuffer("/all-grid/single/grid[@id='").append(gridId).append("']");
		String path = sb.toString();
		Element element = (Element)doc.selectSingleNode(path);
		if(element == null){
			throw new ConfigException("014","配置错误，没有找到 grid id 的对应的 grid 定义：" + gridId);
		}
		return element.attributeValue("table");
	}
	/**
	 * 取得GRID的每页记录数
	 * @param gridId
	 * @return
	 */
	public static int getPageSize(String gridId){
		StringBuffer sb = new StringBuffer("/all-grid/single/grid[@id='").append(gridId).append("']");
		String path = sb.toString();
		Element element = (Element)doc.selectSingleNode(path);
		if(element == null){
			throw new ConfigException("014","配置错误，没有找到 grid id 的对应的 grid 定义：" + gridId);
		}
		String size = element.attributeValue("pageSize");
		if(size == null || !Validation.isInt(size)){
			return PAGE_SIZE;
		}else{
			return Integer.parseInt(size);
		}
	}
	/**
	 * 返回字段列表(List 存储 GridField对象)
	 * @param gridId
	 * @return
	 */
	public static List getSingleGridElements(String gridId){
		List list = new ArrayList();
		StringBuffer sb = new StringBuffer("all-grid/single/grid[@id='").append(gridId).append("']/fields/field");
		String path = sb.toString();
		List eles = doc.selectNodes(path);
		if(eles == null || eles.size() < 0){
			AntiaLog.error("Single Grid id:" + gridId + " not defined any field.");
			return null;
		}
		Element element;
		Output gf;
		for(int i = 0;i<eles.size();i++){
			element = (Element)eles.get(i);
			gf = new Output();
			gf.setField(element.attributeValue("name"));
			gf.setDispMode(element.attributeValue("disp"));
			gf.setGoat(element.attributeValue("goat"));
			gf.setText(element.attributeValue("text"));
			gf.setWidth(element.attributeValue("width"));
			gf.setLinkUrl(element.attributeValue("linkUrl"));
			gf.setOnclick(element.attributeValue("onclick"));
			gf.setTitle(element.attributeValue("title"));
			
			if(gf.getDispMode() != null && !(gf.getDispMode().equals("normal"))){
				if(gf.getGoat() == null){
					gf.setGoat(ConfigConst.DEFAULT_GOAT_STRING);
				}
			}
			//查看是否有要显示的
			Element eleShow = element.element("show");
			if(eleShow != null){
				Element options = eleShow.element("options");
				if(options != null){
					String ref = options.attributeValue("ref");
					if(ref != null && ref.trim().length() > 2 && ref.indexOf(".") > 0){
						String[] refs = ref.split("\\.");
						//options = DBConfiger.gotoOptions(refs[0], refs[1]);
					}
					gf.setValueText(DBConfiger.getDropList(options));
				}
			}
			list.add(gf);
		}
		return list;
	}
	
	/***
	 * 取得指定表格的限制
	 * @param gridId
	 * @return
	 */
	public static String getSingleLimit(String gridId){
		StringBuffer sb = new StringBuffer("all-grid/single/grid[@id='").append(gridId).append("']/limit");
		String path = sb.toString();
		Element element = (Element)doc.selectSingleNode(path);
		if(element == null)
			return null;
		return element.getTextTrim();
	}
	/***
	 * 取得指定表格的SQL语句
	 * @param gridId
	 * @return SQL查询语句
	 */
	public static String getSingleSql(String gridId){
		StringBuffer sb = new StringBuffer("all-grid/single/grid[@id='").append(gridId).append("']/sql");
		String path = sb.toString();
		Element element = (Element)doc.selectSingleNode(path);
		if(element == null)
			return null;
		return element.getTextTrim();
	}
	/**
	 * 取指定表格的扩展属性
	 * @param gridId ID
	 * @return List (OperateField 对象)
	 */
	public static List getSingleExpanded(String gridId){
		StringBuffer sb = new StringBuffer("all-grid/single/grid[@id='").append(gridId).append("']/expanded");
		String path = sb.toString();
		Element element = (Element)doc.selectSingleNode(path);
		if(element == null){
			return null;
		}
		return WindowConfiger.getExpanded(element);
	}
	/***
	 * 取得指定表格的描述
	 * @param gridId
	 * @return 表格的描述
	 */
	public static String getSingleDesc(String gridId){
		StringBuffer sb = new StringBuffer("all-grid/single/grid[@id='").append(gridId).append("']/desc");
		String path = sb.toString();
		Element element = (Element)doc.selectSingleNode(path);
		if(element == null)
			return null;
		return element.getTextTrim();
	}
	
	/***
	 * 取得单表的条件设置
	 * @param gridId
	 * @return
	 */
	public static List getSingleConditions(String gridId){
		List list = new ArrayList();
		StringBuffer sb = new StringBuffer("all-grid/single/grid[@id='").append(gridId).append("']/conditions/field");
		String path = sb.toString();
		List fields = doc.selectNodes(path);
		if(fields == null || fields.size() < 1){
			return null;
		}
		Element element;
		Cele ec;
		for(int i = 0;i<fields.size();i++){
			//循环处理每一个FIELD
			element = (Element)fields.get(i);
			ec = DBConfiger.setAttribute(null, getSingleTable(gridId), element, ConfigConst.FORM_GRID);
			if(ec != null)
				list.add(ec);
		}
		if(list.size() < 1){
			return null;
		}
		return list;
	}
	
	/***
	 * 取单表表格(从配置文件)，这个方法一般只提供给缓存类调用。
	 * 为了提高效率，请从缓存Helper类中调用单表表格配置。因为表格配置将会放入缓存
	 * @param gridId
	 * @return
	 */
	public static SingleGrid getSingleGrid(String gridId){
		SingleGrid grid = new SingleGrid();
		grid.setGridId(gridId);
		grid.setDesc(getSingleDesc(gridId));
		grid.setTable(getSingleTable(gridId));
		grid.setLimit(getSingleLimit(gridId));
		grid.setFields(getSingleGridElements(gridId));
		grid.setSql(getSingleSql(gridId));
		grid.setConditions(getSingleConditions(gridId));
		grid.setExpanded(getSingleExpanded(gridId));
		grid.setPageSize(getPageSize(gridId));
		return grid;
	}
}
